package com.member.web.controller.shop;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.member.common.enums.OrderDeleteEnum;
import com.member.common.utils.StringUtils;
import com.member.shop.domain.SysBizOrderGoods;
import org.springframework.security.access.prepost.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.enums.BusinessType;
import com.member.shop.domain.SysBizRecharge;
import com.member.shop.service.ISysBizRechargeService;
import com.member.common.utils.poi.ExcelUtil;
import com.member.common.core.page.TableDataInfo;

/**
 * 【充值配置】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-05-08
 **/
@RestController
@RequestMapping("/shop/recharge")
public class SysBizRechargeController extends BaseController {
    @Resource
    private ISysBizRechargeService sysBizRechargeService;

    private void setTenantId(SysBizRecharge sysBizRecharge) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizRecharge.setTenantId(tenantId);
        }
    }

    /**
     * 查询充值配置列表
     */
    @PreAuthorize("@ss.hasPermi('shop:recharge:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizRecharge sysBizRecharge) {
        startPage();
        setTenantId(sysBizRecharge);
        List<SysBizRecharge> list = sysBizRechargeService.selectSysBizRechargeList(sysBizRecharge);
        return getDataTable(list);
    }

    /**
     * 导出充值配置列表
     */
    @PreAuthorize("@ss.hasPermi('shop:recharge:export')")
    @Log(title = "充值配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizRecharge sysBizRecharge) {
        setTenantId(sysBizRecharge);
        List<SysBizRecharge> list = sysBizRechargeService.selectSysBizRechargeList(sysBizRecharge);
        ExcelUtil<SysBizRecharge> util = new ExcelUtil<SysBizRecharge>(SysBizRecharge.class);
        util.exportExcel(response, list, "充值配置数据");
    }

    /**
     * 获取充值配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:recharge:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizRechargeService.selectSysBizRechargeById(id));
    }

    /**
     * 新增充值配置
     */
    @PreAuthorize("@ss.hasPermi('shop:recharge:add')")
    @Log(title = "充值配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizRecharge sysBizRecharge) {
        LambdaQueryWrapper<SysBizRecharge> sysBizRechargeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysBizRechargeLambdaQueryWrapper.eq(SysBizRecharge::getName, sysBizRecharge.getName());
        if (!sysBizRechargeService.selectSysBizRechargeList(sysBizRechargeLambdaQueryWrapper).isEmpty()) {
            return AjaxResult.warn("该套餐名称已存在");
        }
        return toAjax(sysBizRechargeService.insertSysBizRecharge(sysBizRecharge));
    }

    /**
     * 修改充值配置
     */
    @PreAuthorize("@ss.hasPermi('shop:recharge:edit')")
    @Log(title = "充值配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizRecharge sysBizRecharge) {
        LambdaQueryWrapper<SysBizRecharge> sysBizRechargeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysBizRechargeLambdaQueryWrapper.eq(SysBizRecharge::getName, sysBizRecharge.getName());
        sysBizRechargeLambdaQueryWrapper.ne(SysBizRecharge::getId, sysBizRecharge.getId());
        if (!sysBizRechargeService.selectSysBizRechargeList(sysBizRechargeLambdaQueryWrapper).isEmpty()) {
            return AjaxResult.warn("该套餐名称已存在");
        }
        return toAjax(sysBizRechargeService.updateSysBizRecharge(sysBizRecharge));
    }

    /**
     * 删除充值配置
     */
    @PreAuthorize("@ss.hasPermi('shop:recharge:remove')")
    @Log(title = "充值配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysBizRechargeService.deleteSysBizRechargeByIds(ids));
    }
}
